class Solution {
    public int distributeCandies(int n, int limit) {
        int num = 0;
        for (int x = 0; x <= Math.min(n, limit); x++) {
            for (int y = 0; y <= Math.min(n - x, limit); y++) {
                int z = n - x - y;
                if (z >= 0 && z <= limit) {
                    num++;
                }
            }
        }
        return num;
